<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Model\M9_good;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cart;
use App\Model\Address;
use App\Models\Order;

class CartController extends Controller
{
    /**
     * 购物车页面
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->session()->has('user')){
            $dat = Cart::content();
            // dd($dat);
            $data = [];
            foreach ($dat as $key => $value) {
                # code...
                $gid = $value->id;
                $num = $value->qty;
                $rowId = $value->rowId;
                $good = M9_good::find($gid);
                $good['rowId'] = $rowId;
                $good['gnum'] = $num;
                $data[] = $good;
            }
            // dd($good);
            // dd($data);
            return view('home.shopcart',compact('data'));
        } else {
            return view('home.login');
        }
        
    }

    /**
     * 处理提交订单的逻辑业务
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {   
        $data = $request->input('data');
        if(!empty($data)){
            $datas = [];
            foreach ($data as $key => $value) {
                # code...
                // 获取要购买的商品

                //var_dump($value);
                $datas[] = Cart::get($value);
                // var_dump($datas);
            }
            // dd(1);
           
            // 清空购物车
            Cart::destroy();
            $cart = [];
            foreach ($datas as $key => $value) {
                # code...
                $cart[$key]['id'] = $value->id;
                $cart[$key]['qty'] = $value->qty;
                $cart[$key]['name'] = $value->name;
                $cart[$key]['price'] = $value->price;

            }
           
            Cart::add($cart);
             //$b=Cart::get('620d670d95f0419e35f9182695918c68');
            
            // $pay = Cart::content();
            // dd($pay);
            $sum = $request->input('sum');
            $val[0] = 1;
            $val[1] = $sum;  
            return $val;
        }else{
            $val[0] = 0;
            $val[1] = false;  
            return $val;
        }
    }

    /**
     * 加载付款页面(ajax跳转过来)
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function payGood(Request $request,$sum) 
    {   
        // 获取要购买的商品
        $sum = $sum;
        $car=Cart::content();
        // dd($car);
        $data = [];
        foreach ($car as $key => $value) {
            # code...
            $gid = $value->id;
            $num = $value->qty;
            $rowId = $value->rowId;
            $good = M9_good::find($gid);
            // rowid 和 商品数量
            $good['rowId'] = $rowId;
            $good['gnum'] = $num;
            // 获取商品总价格
            // $price = $value->price;
            // $sum = $num * $price;


            $data[] = $good;
        }  
        // 获取购买商品的用户地址信息
        $user = session('user');
        // 登录用户的id
        $uid = $user->uid;
        // dd($uid);
        $address = Address::where('b_uid',$uid)->get();
        // dd($address);
        
        return view('home.pay',compact('data','address','sum'));    
    }

    /**
     * 商品添加到购物车
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // dd($request->session()->has('user'));
        // dd($request->session('user'));
        // dd($request->all());
        if ($request->session()->has('user')) {
             $da = $request->except('_token');
            // dd($data);
            // array:2 [
            //     "num" => "3"
            //     "gid" => "16"
            // ]
            $data = M9_good::find($da['gid']);

            $res['id'] = $data['gid'];
            $res['name'] = $data['gname'];
            $res['qty'] = $da['num'];
            $res['price'] = $data['gcost'];

            // dd($res);
            Cart::add($res);
            return 1;
        } else {
            return 2;
        }
       

    }

    /**
     * 删除购物车商品的方法
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        Cart::remove($id);
        return 1;
    }

    /**
     * 购物车商品增加数量的时候
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = Cart::get($id);
       $gedit = $data -> qty;
       if($gedit < 5){
           $num = $gedit + 1;
           Cart::update($id,$num);
           return 1;
       }
    }

    /**
     * 购物车商品减少数量的时候
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
       $data = Cart::get($id);
       $gedit = $data -> qty;
       if($gedit > 1){
           $num = $gedit - 1;
           Cart::update($id,$num);
           return 1;
       }
       
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 提交订单，生成订单
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    function dobill (Request $request)
    {   
        $prices = $request->input('prices');
        // 获取订单地址
        $pid = $request->input('pid');
        $bill = Address::find($pid);
        // dd($bill->bphone);
        $oaddress = $bill->baddress;
        $o_name = $bill->bconsignee;
        $o_phone = $bill->bphone;
        // 获取用户id
        $user = session('user');
        // dd($user->uid);
        $o_uid = $user->uid;
        // 获取购物车商品
        $order = Cart::content();
        // dd($order);
        // 生成随机订单编号
        $onumber = time().rand(100000,999999);
        $otime = time();
        foreach ($order as $key => $value) {
            # code...
            $orders = new Order;
            $rowId = $value->rowId;
            $o_gid = $value->id;
            $onum = $value->qty;
            // 添加数据到数据库
            $orders->o_gid = $o_gid;
            $orders->o_uid = $o_uid;
            $orders->onum = $onum;
            $orders->onumber = $onumber;
            $orders->otime = $otime;
            $orders->oaddress = $oaddress;
            $orders->o_name = $o_name;
            $orders->o_phone = $o_phone;
            $orders->o_prices = $prices;
            $orders->save();
            Cart::remove($rowId);
        }
        $data = Cart::content();
        // dd(unset($data));

        // 判断对象的第一个值是否为空
        if(empty($data->first())){
            $numb[0] = 1;
            $numb[1] = $onumber; 
            return $numb;
        }else{
            $numb[0] = 0;
            $numb[1] = 'errors';
            return $numb;
        }
    }

    /**
     * 跳转订单提交成功的页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    function endpay($id)
    {
        // echo $id;
        $orders = Order::where('onumber',$id)->get();
        // dd($orders[0]);
        $orders = $orders[0];
        return view('home.success',compact('orders'));
    }

}
